package com.smartcommunity.controller.vidInfo;

import com.smartcommunity.bean.entity.User;
import com.smartcommunity.bean.model.Pager;
import com.smartcommunity.controller.BaseController;
import com.smartcommunity.entity.vid.SearchModel;
import com.smartcommunity.entity.vid.VidInfoModel;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.CommonService;
import com.smartcommunity.service.vidInfo.impl.VidInfoServiceImpl;
import com.smartcommunity.utils.ArrayUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PeopleController
 * @Description: 视频档案控制层
 */
@RestController
@RequestMapping(value = "/vidInfo")
@Api(value = "小区服务-视频档案", tags = "视频档案")
@Slf4j
public class VidInfoController extends BaseController {

    @Autowired
    private VidInfoServiceImpl vidInfoService;

    @Autowired
    private CommonService commonService;

    /**
     * 视频档案查询
     *
     * @return
     */
    @Log(info = "查询视频档案列表", module = "视频档案", function = "查询")
    @PostMapping(value = "/pageVidInfo")
    @ApiOperation(value = "视频档案列表", notes = "视频档案列表")
    public Pager<VidInfoModel> pageAllVidInfo(@RequestBody SearchModel searchModel) {

        if (StringUtils.isBlank(searchModel.getVillageCode())) {
            User user = getCurrentUser();
            String villageCode = commonService.getVillageCodeByUser(user);
            if (StringUtils.isNotEmpty(searchModel.getParentId())) {
                String orgIds = commonService.getAllChildOrgId(searchModel.getParentId());
                Map<String, Object> map = new HashedMap();
                map.put("orgId", orgIds);
                map.put("villageCode", villageCode);
                List<String> villageList = commonService.listVillageCodeByOrgId(map);
                //此行政区划节点下没有小区，则返回
                if (CollectionUtils.isEmpty(villageList)){
                    return new Pager<>();
                }
                villageCode = ArrayUtils.toString(villageList);
            }
            searchModel.setVillageCode(villageCode.replaceAll("'", ""));
        }

        return vidInfoService.listPage(searchModel);
    }
}
